home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / sail.tar / sail / dr_2.c < prev    next >
C/C++ Source or Header  |  1992-09-18  |  6KB  |  255 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that this notice is preserved and that due credit is given
  7.  * to the University of California at Berkeley. The name of the University
  8.  * may not be used to endorse or promote products derived from this
  9.  * software without specific prior written permission. This software
  10.  * is provided ``as is'' without express or implied warranty.
  11.  */
  12.  
  13. #ifndef lint
  14. static char sccsid[] = "@(#)dr_2.c    5.2 (Berkeley) 3/9/88";
  15. #endif /* not lint */
  16.  
  17. #include "driver.h"
  18.  
  19. #define couldwin(f,t) (f->specs->crew2 > t->specs->crew2 * 1.5)
  20.  
  21. thinkofgrapples()
  22. {
  23.     register struct ship *sp, *sq;
  24.     char friendly;
  25.  
  26.     foreachship(sp) {
  27.         if (sp->file->captain[0] || sp->file->dir == 0)
  28.             continue;
  29.         foreachship(sq) {
  30.             friendly = sp->nationality == capship(sq)->nationality;
  31.             if (!friendly) {
  32.                 if (sp->file->struck || sp->file->captured != 0)
  33.                     continue;
  34.                 if (range(sp, sq) != 1)
  35.                     continue;
  36.                 if (grappled2(sp, sq))
  37.                     if (toughmelee(sp, sq, 0, 0))
  38.                         ungrap(sp, sq);
  39.                     else
  40.                         grap(sp, sq);
  41.                 else if (couldwin(sp, sq)) {
  42.                     grap(sp, sq);
  43.                     sp->file->loadwith = L_GRAPE;
  44.                 }
  45.             } else
  46.                 ungrap(sp, sq);
  47.         }
  48.     }
  49. }
  50.  
  51. checkup()
  52. {
  53.     register struct ship *sp, *sq;
  54.     register char explode, sink;
  55.  
  56.     foreachship(sp) {
  57.         if (sp->file->dir == 0)
  58.             continue;
  59.         explode = sp->file->explode;
  60.         sink = sp->file->sink;
  61.         if (explode != 1 && sink != 1)
  62.             continue;
  63.         if (die() < 5)
  64.             continue;
  65.         Write(sink == 1 ? W_SINK : W_EXPLODE, sp, 0, 2, 0, 0, 0);
  66.         Write(W_DIR, sp, 0, 0, 0, 0, 0);
  67.         if (snagged(sp))
  68.             foreachship(sq)
  69.                 cleansnag(sp, sq, 1);
  70.         if (sink != 1) {
  71.             makesignal(sp, "exploding!", (struct ship *)0);
  72.             foreachship(sq) {
  73.                 if (sp != sq && sq->file->dir && range(sp, sq) < 4)
  74.                     table(RIGGING, L_EXPLODE, sp->specs->guns/13, sq, sp, 6);
  75.             }
  76.         } else
  77.             makesignal(sp, "sinking!", (struct ship *)0);
  78.     }
  79. }
  80.  
  81. prizecheck()
  82. {
  83.     register struct ship *sp;
  84.  
  85.     foreachship(sp) {
  86.         if (sp->file->captured == 0)
  87.             continue;
  88.         if (sp->file->struck || sp->file->dir == 0)
  89.             continue;
  90.         if (sp->specs->crew1 + sp->specs->crew2 + sp->specs->crew3 > sp->file->pcrew * 6) {
  91.             Write(W_SIGNAL, sp, 1,
  92.                 (int)"prize crew overthrown", 0, 0, 0);
  93.             Write(W_POINTS, sp->file->captured, 0, sp->file->captured->file->points - 2 * sp->specs->pts, 0, 0, 0);
  94.             Write(W_CAPTURED, sp, 0, -1, 0, 0, 0);
  95.         }
  96.     }
  97. }
  98.  
  99. strend(str)
  100. char *str;
  101. {
  102.     register char *p;
  103.  
  104.     for (p = str; *p; p++)
  105.         ;
  106.     return p == str ? 0 : p[-1];
  107. }
  108.  
  109. closeon(from, to, command, ta, ma, af)
  110. register struct ship *from, *to;
  111. char command[];
  112. int ma, ta, af;
  113. {
  114.     int high;
  115.     char temp[10];
  116.  
  117.     temp[0] = command[0] = '\0';
  118.     high = -30000;
  119.     try(command, temp, ma, ta, af, ma, from->file->dir, from, to, &high, 0);
  120. }
  121.  
  122. int dtab[] = {0,1,1,2,3,4,4,5};        /* diagonal distances in x==y */
  123.  
  124. score(movement, ship, to, onlytemp)
  125. char movement[];
  126. register struct ship *ship, *to;
  127. char onlytemp;
  128. {
  129.     char drift;
  130.     int row, col, dir, total, ran;
  131.     register struct File *fp = ship->file;
  132.  
  133.     if ((dir = fp->dir) == 0)
  134.         return 0;
  135.     row = fp->row;
  136.     col = fp->col;
  137.     drift = fp->drift;
  138.     move(movement, ship, &fp->dir, &fp->row, &fp->col, &drift);
  139.     if (!*movement)
  140.         (void) strcpy(movement, "d");
  141.  
  142.     ran = range(ship, to);
  143.     total = -50 * ran;
  144.     if (ran < 4 && gunsbear(ship, to))
  145.         total += 60;
  146.     if ((ran = portside(ship, to, 1) - fp->dir) == 4 || ran == -4)
  147.         total = -30000;
  148.  
  149.     if (!onlytemp) {
  150.         fp->row = row;
  151.         fp->col = col;
  152.         fp->dir = dir;
  153.     }
  154.     return total;
  155. }
  156.  
  157. move(p, ship, dir, row, col, drift)
  158. register char *p;
  159. register struct ship *ship;
  160. register char *dir;
  161. register short *row, *col;
  162. register char *drift;
  163. {
  164.     int dist;
  165.     char moved = 0;
  166.  
  167.     for (; *p; p++) {
  168.         switch (*p) {
  169.         case 'r':
  170.             if (++*dir == 9)
  171.                 *dir = 1;
  172.             break;
  173.         case 'l':
  174.             if (--*dir == 0)
  175.                 *dir = 8;
  176.             break;
  177.         case '1': case '2': case '3': case '4':
  178.         case '5': case '6': case '7':
  179.             moved++;
  180.             if (*dir % 2 == 0)
  181.                 dist = dtab[*p - '0'];
  182.             else
  183.                 dist = *p - '0';
  184.             *row -= dr[*dir] * dist;
  185.             *col -= dc[*dir] * dist;
  186.             break;
  187.         }
  188.     }
  189.     if (!moved) {
  190.         if (windspeed != 0 && ++*drift > 2) {
  191.             if (ship->specs->class >= 3 && !snagged(ship)
  192.                 || (turn & 1) == 0) {
  193.                 *row -= dr[winddir];
  194.                 *col -= dc[winddir];
  195.             }
  196.         }
  197.     } else
  198.         *drift = 0;
  199. }
  200.  
  201. try(command, temp, ma, ta, af, vma, dir, f, t, high, rakeme)
  202. register struct ship *f, *t;
  203. int ma, ta, af, *high, rakeme;
  204. char command[], temp[];
  205. {
  206.     register int new, n;
  207.     char st[4];
  208. #define rakeyou (gunsbear(f, t) && !gunsbear(t, f))
  209.  
  210.     if ((n = strend(temp)) < '1' || n > '9')
  211.         for (n = 1; vma - n >= 0; n++) {
  212.             (void) sprintf(st, "%d", n);
  213.             (void) strcat(temp, st);
  214.             new = score(temp, f, t, rakeme);
  215.             if (new > *high && (!rakeme || rakeyou)) {
  216.                 *high = new;
  217.                 (void) strcpy(command, temp);
  218.             }
  219.             try(command, temp, ma-n, ta, af, vma-n,
  220.                 dir, f, t, high, rakeme);
  221.             rmend(temp);
  222.         }
  223.     if (ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r' || !strlen(temp)) {
  224.         (void) strcat(temp, "r");
  225.         new = score(temp, f, t, rakeme);
  226.         if (new > *high && (!rakeme || gunsbear(f, t) && !gunsbear(t, f))) {
  227.             *high = new;
  228.             (void) strcpy(command, temp);
  229.         }
  230.         try(command, temp, ma-1, ta-1, af, min(ma-1, maxmove(f, (dir == 8 ? 1 : dir+1), 0)), (dir == 8 ? 1 : dir+1),f,t,high,rakeme);
  231.         rmend(temp);
  232.     }
  233.     if ((ma > 0 && ta > 0 && (n = strend(temp)) != 'l' && n != 'r') || !strlen(temp)){
  234.         (void) strcat(temp, "l");
  235.         new = score(temp, f, t, rakeme);
  236.         if (new > *high && (!rakeme || (gunsbear(f, t) && !gunsbear(t, f)))){
  237.             *high = new;
  238.             (void) strcpy(command, temp);
  239.         }
  240.         try(command, temp, ma-1, ta-1, af, (min(ma-1,maxmove(f, (dir-1 ? dir-1 : 8), 0))), (dir-1 ? dir -1 : 8), f, t, high, rakeme);
  241.         rmend(temp);
  242.     }
  243. }
  244.  
  245. rmend(str)
  246. char *str;
  247. {
  248.     register char *p;
  249.  
  250.     for (p = str; *p; p++)
  251.         ;
  252.     if (p != str)
  253.         *--p = 0;
  254. }
  255.